package com.cloud.pole.user.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.pole.api.order.OrderApi;
import com.cloud.pole.api.user.BaseUserApi;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.rest.BaseController;
import com.cloud.pole.common.util.RegexUtils;
import com.cloud.pole.common.util.jwt.IJWTInfo;
import com.cloud.pole.common.util.jwt.JWTInfo;
import com.cloud.pole.common.util.jwt.JwtTokenUtil;
import com.cloud.pole.user.biz.BaseUserBiz;
import com.cloud.pole.user.config.FileUploadConfiguration;
import com.cloud.pole.user.model.entity.BaseUser;
import com.cloud.pole.user.service.AuthService;
import com.cloud.pole.user.service.UserService;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("user/front")
@Slf4j
public class UserController{

	@Autowired
	private UserService userService;
	@Autowired
	private FileUploadConfiguration fileUploadConfiguration;
	
	/**
	 * 修改登录密码
	 * @param loginPwd
	 * @param newLoginPwd
	 * @param token
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/modifyLoginPwd")
	public ApiRestResponse<String> modifyLoginPwd(			
			@RequestParam(defaultValue = "") String loginPwd,
			@RequestParam(defaultValue = "") String newLoginPwd,
			@RequestHeader(defaultValue = "") String token)throws Exception{
		try {
			ParamException.err(StringUtils.isBlank(loginPwd), "登录密码不能为空");
			ParamException.err(StringUtils.isBlank(newLoginPwd), "新登录密码不能为空");
			ParamException.err(StringUtils.isBlank(token), "用户登陆token");
			userService.modifyLoginPwd(loginPwd, newLoginPwd, token);			
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, null);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE +"{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
	}
	/**
	 * 修改支付密码
	 * @param payPwd
	 * @param newPayPwd
	 * @param token
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/modifyPayPwd")
	public ApiRestResponse<Boolean> modifyPayPwd(
			@RequestParam(name = "payPwd", defaultValue = "") String payPwd,
			@RequestParam(name = "newPayPwd", defaultValue = "") String newPayPwd,
			@RequestHeader(name = "token", defaultValue = "") String token) throws Exception{
		try {
			ParamException.err(StringUtils.isBlank(payPwd), "payPwd不能为空");
			ParamException.err(StringUtils.isBlank(newPayPwd), "newPayPwd不能为空");
			ParamException.err(StringUtils.isBlank(token), "token不能为空");
			userService.modifyPayPwd(payPwd, newPayPwd, token);			
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, null);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE +"{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
	}
	
	/**
	 * 修改支付密码
	 * @param payPwd
	 * @param newPayPwd
	 * @param token
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/modifyUser")
	public ApiRestResponse<Boolean> modifyUser(
			@RequestHeader(name = "token", defaultValue = "") String token,
			@RequestParam(name = "iconPath", defaultValue = "") String iconPath,
			@RequestParam(name = "realName", defaultValue = "") String realName,
			@RequestParam(name = "sex", defaultValue = "") String sex,
			@RequestParam(name = "birthday", defaultValue = "") String birthday,
			@RequestParam(name = "provinceId", defaultValue = "") String provinceId,
			@RequestParam(name = "provinceName", defaultValue = "") String provinceName,
			@RequestParam(name = "cityId", defaultValue = "") String cityId,
			@RequestParam(name = "cityName", defaultValue = "") String cityName,
			@RequestParam(name = "districtId", defaultValue = "") String districtId,
			@RequestParam(name = "districtName", defaultValue = "") String districtName,
			@RequestParam(name = "weixin", defaultValue = "") String weixin) throws Exception{
		try {
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userService.modifyUser(token, iconPath, realName, sex, birthday, provinceId, provinceName, cityId, cityName, districtId, districtName, weixin));
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE +"{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
	}
	
    @RequestMapping(value = "loginOut")
    public ApiRestResponse<Boolean> loginOut(
    		@RequestHeader(defaultValue = "") String token) throws Exception {
        try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");
	        return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE,  userService.loginOut(token));
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error("登录错误{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
    
    @RequestMapping(value = "getUser")
    public ApiRestResponse<BaseUserApi> getUser(
    		@RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");
			BaseUserApi userVo = userService.getBaseUser(token);
	        return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userVo);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE +"{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
}
